<?php 
#clean extjs datas sended by propertygrid

## $this->ctrl have a control this call

if (false) {
} else if ($this->ctrl == "save") {
	# mange save item
	/*
	foreach($_POST as $k=>$v) {
		if (substr($k,0,1)=="(") {
			$h = substr($k,4);
			$_POST[$h] = $v;
			unset($_POST[$k]);
		}
		if (substr($k,0,3)=="Ext") {
			$u = explode("-",$k,2);
			if (substr($v,0,4)=="(ID:") {
				$v = (int)substr($v,4);
			}
			$h = substr($u[1],4);
			$_POST[$h] = $v;
			unset($_POST[$k]);
		}
	}
	$class = $_POST["oh-class"];unset($_POST["oh-class"]);
	*/
	#save or create new object
	/*
	if ($this->item>0) {
				
	} else {
		$data = oh("simpleform")->get_post_data();
		$item = oh("dmodel")->new_object($class,$data);	
		echo $item;
	}
	*/
	$ret = array(
		"success"=>true,
		"msg" => $_POST,
	);
	echo json_encode($ret);

} else if ($this->ctrl == "save-node") {
	$class = $_POST["form"];
	$id = isset($_POST["id"]) ? (int)$_POST["id"] : 0;
	$root = isset($_POST["item"]) ? (int)$_POST["item"] : 0;
	$field = isset($_POST["attr"]) ? $_POST["attr"] : "null";
	$data = oh("simpleform")->get_post_data();			
	if ($id>0) {
	
	} else {
		if ($root>0) {
			$id = oh("dmodel")->new_object("{$class}&root={$root}",$data);	
			oh("dmodel")->set_meta($root,$field,$id);
		} else {
			$id = oh("dmodel")->new_object($class,$data);	
		}
	}
	echo $id;
	/*
	foreach($_POST as $k=>$v) {
		if (substr($k,0,1)=="(") {
			$h = substr($k,4);
			$_POST[$h] = $v;
			unset($_POST[$k]);
		}
		if (substr($k,0,3)=="Ext") {
			$u = explode("-",$k,2);
			if (substr($v,0,4)=="(ID:") {
				$v = (int)substr($v,4);
			}
			$h = substr($u[1],4);
			$_POST[$h] = $v;
			unset($_POST[$k]);
		}
	}
	$class = $_POST["oh-class"];unset($_POST["oh-class"]);
	#save or create new object
	if ($this->item>0) {
				
	} else {
		$data = oh("simpleform")->get_post_data();
		$item = oh("dmodel")->new_object($class,$data);	
		echo $item;
	}
	*/
	
} else if ($this->ctrl == "delete") {
	$ids = $_POST["ids"];
	$ids = explode(",",$ids);
	if (count($ids)>0) {
		foreach($ids as $id) {
			oh("dmodel")->del_object("id={$id}");	
		}
	}

} else if ($this->ctrl == "update") {
	echo "ciao";
	
}